package sit.engreal.leetcode.problems700;

import sit.engreal.leetcode.util.TreeNode;

/**
 * <a href="https://leetcode.cn/problems/merge-two-binary-trees/">617. 合并二叉树</a>
 */
public class LeetCode617 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null && root2 == null) {
            return null;
        } else if (root1 == null) {
            return new TreeNode(
                    root2.val,
                    mergeTrees(root2.left, null),
                    mergeTrees(root2.right, null));
        } else if (root2 == null) {
            return new TreeNode(
                    root1.val,
                    mergeTrees(root1.left, null),
                    mergeTrees(root1.right, null));
        } else {
            return new TreeNode(
                    root1.val + root2.val,
                    mergeTrees(root1.left, root2.left),
                    mergeTrees(root1.right, root2.right));
        }
    }
}
